#####################################################################
#
#                          C++ Assistant
#
# 
# Author : Guillaume Delhumeau <guillaume.delhumeau@gmail.com>
# License : GNU General Public License 3
#
#####################################################################

import gtk
import gobject
import File
import Project
import gedit
import Navigator

class SearchFileDialog(gtk.Dialog):
	""" Dialog which displays all files of the project, with a regexp filter """
	
	def __init__(self, parent, project):
		""" Constructor """
		gtk.Dialog.__init__(self, "Search in project [C++ Assistant]", parent, gtk.DIALOG_DESTROY_WITH_PARENT)
		
		# Init
		self.project = project
		self.winparent = parent		
		
		# Size
		self.set_default_size(600, 400)	
				
		# Create the model.
		self.model = gtk.TreeStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING, gobject.TYPE_STRING)
		for f in project._files:
			iter = self.model.insert_before(None, None)
			self.model.set_value(iter, 0, f)
			self.model.set_value(iter, 1, f.basename)
			self.model.set_value(iter, 2, f.uri)
	    
	    # View
		self.view = gtk.TreeView(self.model)
		renderer = gtk.CellRendererText()
		column_filenames = gtk.TreeViewColumn("Files", renderer, text=1)
		column_file_uri = gtk.TreeViewColumn("Path", renderer, text=2)
		self.view.append_column(column_filenames)
		self.view.append_column(column_file_uri)
		self.view.set_enable_search(False)
		self.view.get_selection().set_mode(gtk.SELECTION_SINGLE)
		self.view.show()	
		scrolled = gtk.ScrolledWindow()
		scrolled.add(self.view)
		scrolled.show()		
		self.vbox.pack_start(scrolled)
		
		# Hbox
		self.query_hbox = gtk.HBox()
		self.vbox.pack_end(self.query_hbox, expand=False, fill=True)
		self.query_hbox.show()
		
		# Query 
		self.query = gtk.Entry()
		self.query_hbox.pack_start(self.query, expand=True, fill=True)
		self.query.show()
		self.set_focus(self.query)
		
		# Buttons
		self.bt_ok = gtk.Button("OK")
		self.query_hbox.pack_end(self.bt_ok, expand=False, fill=True)
		self.bt_ok.show()
		
		# Signals
		self.connect_object("destroy_event", self.on_quit, self)
		self.query.connect_object("key_release_event", self.on_query_key_release, self)
		self.bt_ok.connect_object("button_release_event", self.on_button_release, self)
		self.connect_object("key_release_event", self.on_key_release, self)

		# Selection		
		self.view.get_selection().select_iter(self.model.get_iter_first())
		
	def on_quit(self, widget):
		""" On quit event """
		print "bla"
		
	def on_query_key_release(self, widget, event):
		""" Method called when a key has been released on the Gtk.Entry - Updating the file filter"""		
		
		if event.keyval <> 65364 and event.keyval <> 65362:
			query = self.query.get_text()
			if(len(query)>0):	
				files = self.project.get_files_from_keywords(query.split(" "))		 
				self.model.clear()		
				for f in files:
					iter = self.model.insert_before(None, None)
					self.model.set_value(iter, 0, f)
					self.model.set_value(iter, 1, f.basename)
					self.model.set_value(iter, 2, f.path)
			else:
				self.model.clear()
				for f in self.project._files:
					iter = self.model.insert_before(None, None)
					self.model.set_value(iter, 0, f)
					self.model.set_value(iter, 1, f.basename)
					self.model.set_value(iter, 2, f.path)
			
		
			self.view.get_selection().select_iter(self.model.get_iter_first())
	
	def on_button_release(self, widget, event):
		""" On button release """
		self.on_valid()
	
	def on_valid(self):
		""" On validating form """
		model,it = self.view.get_selection().get_selected()
		if it <> None:
			value = self.model.get_value(it, 0)
			Navigator.switch_to(self.winparent, value.uri)
		self.destroy()
		
		
	def on_key_release(self, widget, event):
		""" On global key release """
		### ON ENTER KEY
		if event.keyval == 65293 or event.keyval == 65421:
			self.on_valid()
		### ON DOWN KEY
		elif event.keyval == 65364:
			model,it = self.view.get_selection().get_selected()			
			it_new = model.iter_next(it)			
			self.view.get_selection().select_iter(it_new)
			self.set_focus(self.query)
		### ON UP KEY
		elif event.keyval == 65362:
			model,it = self.view.get_selection().get_selected()			
			it_new = model.get_iter_first()
			while model.get_path(model.iter_next(it_new)) <> model.get_path(it):
				it_new = model.iter_next(it_new)		
			self.view.get_selection().select_iter(it_new)
			self.set_focus(self.query)
		### Else managing focus
		else:
			if(self.get_focus()<>self.query):
				self.set_focus(self.query)

